package com.assistant.wechat.service.impl;

import com.assistant.admin.pojo.PageResult;
import com.assistant.common.DateUtil;
import com.assistant.wechat.mapper.LoginMapper;
import com.assistant.wechat.mapper.UserInfoMapper;
import com.assistant.wechat.pojo.UserInfo;
import com.assistant.wechat.pojo.WxUser;
import com.assistant.wechat.service.UserInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class UserInfoServiceImpl implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private LoginMapper loginMapper;

    @Override
    public int insertUserInfo(UserInfo userInfo) {
        userInfo.setPost_time(DateUtil.dataFormatUtil(new Date()));
        return userInfoMapper.insertSelective(userInfo);
    }

    @Override
    @Transactional
    public int updateUserInfoStatus(Integer id, String openid) {
        UserInfo userInfo = new UserInfo();
        userInfo.setStatus(1);
        userInfo.setId(id);
        userInfo.setVerifytime(DateUtil.dataFormatUtil(new Date()));
        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if (i == 1) {
            WxUser user = new WxUser();
            user.setOpenid(openid);
            user.setAuth(1);
            Example example = new Example(WxUser.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("openid", openid);
            loginMapper.updateByExampleSelective(user, example);
        }
        return i;
    }

    @Override
    @Transactional
    public int updateUserInfoRefuse(Map<String, String> map) {
        UserInfo userInfo = new UserInfo();
        userInfo.setId(Integer.valueOf(map.get("id")));
        userInfo.setMsg(map.get("msg"));
        userInfo.setStatus(2);
        userInfo.setVerifytime(DateUtil.dataFormatUtil(new Date()));
        int i = userInfoMapper.updateByPrimaryKeySelective(userInfo);
        if (i == 1) {
            WxUser user = new WxUser();
            user.setOpenid(map.get("wx_id"));
            user.setAuth(0);
            Example example = new Example(WxUser.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("openid", map.get("wx_id"));
            loginMapper.updateByExampleSelective(user, example);
        }
        return i;
    }

    @Override
    public PageResult<UserInfo> getUserInfoList(Integer page, Integer rows, String key, Integer status) {
        Example example = new Example(UserInfo.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(key)) {
            criteria.andLike("num", "%" + key + "%").orEqualTo("num", key);
        }
        if (status != 0) {
            criteria.andEqualTo("status", status);
        }
        criteria.andEqualTo("validated",1);
        PageHelper.startPage(page, rows);
        List<UserInfo> list = userInfoMapper.selectByExample(example);
        PageInfo<UserInfo> brandPageInfo = new PageInfo<UserInfo>(list);
        return new PageResult<>(brandPageInfo.getTotal(), brandPageInfo.getList());
    }

    @Override
    public int delUserInfo(Integer id) {
        return userInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<UserInfo> getUserInfo(String openid) {
        Example example = new Example(UserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wx_id", openid);
        criteria.andEqualTo("validated",1);
        return userInfoMapper.selectByExample(example);
    }

    @Override
    public List<UserInfo> getUserInfoById(Integer id) {
        Example example = new Example(UserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        criteria.andEqualTo("validated",1);
        return userInfoMapper.selectByExample(example);
    }

    @Override
    public int UpdateValidated(String id) {
        Example example = new Example(UserInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wx_id",id);
        UserInfo userInfo = new UserInfo();
        userInfo.setValidated(0);
        return userInfoMapper.updateByExampleSelective(userInfo,example);
    }


}
